package com.jsoft.dao.impl;

import com.jsoft.dao.ContractVoDao;
import com.jsoft.entiy.Check;
import com.jsoft.entiy.vo.ContractVo;
import com.jsoft.util.DAOImpl;

import java.util.List;

/**
 * @program: CPM
 * @description:
 * @author: beibei
 * @create: 2022-09-08 15:02
 */
public class ContractVoDaoImpl extends DAOImpl<ContractVo> implements ContractVoDao {

    //查询未付款的合同部分信息
    @Override
    public List<ContractVo> getUnpaidContractInfo() {
        List<ContractVo> forList = null;
        String sql = "SELECT con.`id` `id`,cli.`c_name` `sellName`,t.`c_name` `buyName`,con.`money`,con.`startDate`,con.`paystate` " +
                "FROM contract con " +
                "JOIN `client` cli ON con.`buyId` = cli.`c_id` " +
                "JOIN (SELECT con.`id`,cli.`c_name`" +
                "FROM contract con " +
                "JOIN `client` cli ON con.`sellId` = cli.`c_id` ) t ON con.`id` = t.id WHERE con.`paystate` = '未付款';";
        try {
            forList = getForList(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;
    }

    //查询已付款的合同部分信息
    @Override
    public List<ContractVo> getPaidContractInfo() {
        List<ContractVo> forList = null;
        String sql = "SELECT con.`id` `id`,cli.`c_name` `sellName`,t.`c_name` `buyName`,con.`money`,con.`startDate`,con.`paystate` " +
                "FROM contract con " +
                "JOIN `client` cli ON con.`buyId` = cli.`c_id` " +
                "JOIN (SELECT con.`id`,cli.`c_name`" +
                "FROM contract con " +
                "JOIN `client` cli ON con.`sellId` = cli.`c_id` ) t ON con.`id` = t.id WHERE con.`paystate` = '已付款';";
        try {
            forList = getForList(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;
    }

    //查询续费的合同部分信息
    @Override
    public List<ContractVo> getRenewalContractInfo() {
        List<ContractVo> forList = null;
        String sql = "SELECT con.`id` `id`,cli.`c_name` `sellName`,t.`c_name` `buyName`,con.`money`,con.`startDate`,con.`paystate` " +
                "FROM contract con " +
                "JOIN `client` cli ON con.`buyId` = cli.`c_id` " +
                "JOIN (SELECT con.`id`,cli.`c_name`" +
                "FROM contract con " +
                "JOIN `client` cli ON con.`sellId` = cli.`c_id` ) t ON con.`id` = t.id WHERE con.`type` = '续费合同';";
        try {
            forList = getForList(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;
    }

    //条件查询
    @Override
    public List<ContractVo> ConditionalQuery(Check check) {
        List<ContractVo> forList = null;
        String sql = "SELECT con.`id` `id`,cli.`c_name` `sellName`,t.`c_name` `buyName`,con.`money`,con.`startDate`,con.`paystate` " +
                "FROM contract con " +
                "JOIN `client` cli ON con.`buyId` = cli.`c_id` " +
                "JOIN (SELECT con.`id`,cli.`c_name`" +
                "FROM contract con " +
                "JOIN `client` cli ON con.`sellId` = cli.`c_id` ) t ON con.`id` = t.id";
        try {

        if (check.getName()!=null && check.getType()==null && check.getStartTime()==null){
            sql = sql+" WHERE cli.`c_name` = ?";
            forList = getForList(sql,check.getName());
        }
        if (check.getName()==null && check.getType()!=null && check.getStartTime()==null){
            sql = sql+" WHERE con.`type` = ?;";
            forList = getForList(sql,check.getType());
        }
        if (check.getName()==null && check.getType()==null && check.getStartTime()!=null){
            sql = sql+" WHERE con.`startDate` BETWEEN ? AND ?;";
            forList = getForList(sql,check.getStartTime(),check.getEndTime());
        }
        if (check.getName()!=null && check.getType()!=null && check.getStartTime()==null){
            sql = sql+" WHERE cli.`c_name` = ? AND con.`type` = ? ;";
            forList = getForList(sql,check.getName(),check.getType());
        }
        if (check.getName()!=null && check.getType()==null && check.getStartTime()!=null){
            sql = sql+" WHERE cli.`c_name` = ? AND con.`startDate` BETWEEN ? AND ?;";
            forList = getForList(sql,check.getName(),check.getStartTime(),check.getEndTime());
        }
        if (check.getName()==null && check.getType()!=null && check.getStartTime()!=null){
            sql = sql+" WHERE con.`type` = ? AND con.`startDate` BETWEEN ? AND ?;";
            forList = getForList(sql,check.getType(),check.getStartTime(),check.getEndTime());
        }
        if (check.getName()!=null && check.getType()!=null && check.getStartTime()!=null){
            sql = sql+" WHERE cli.`c_name` = ? AND con.`type` = ? AND con.`startDate` BETWEEN ? AND ?;";
            forList = getForList(sql,check.getName(),check.getType(),check.getStartTime(),check.getEndTime());
        }
            System.out.println(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;

    }
}